package com.huanong.it.fooder.controller;

import com.huanong.it.fooder.dto.UserLoginDto;
import com.huanong.it.fooder.dto.UserRegisterDto;
import com.huanong.it.fooder.dto.UserResponseDto;
import com.huanong.it.fooder.response.ApiResponse;
import com.huanong.it.fooder.service.UserService;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.validation.Valid;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;
    private static final Logger log = LogManager.getLogger(UserController.class);
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    // 用户注册接口
    @PostMapping("/register")
    public ApiResponse<?> register(@Valid @RequestBody UserRegisterDto dto) {
        log.info("注册请求数据: username={}, email={}", dto.getUsername(), dto.getEmail());
        return userService.register(dto);
    }

    // 用户登录接口
    @PostMapping("/login")
    public ApiResponse<?> login(@Valid @RequestBody UserLoginDto dto) {
        log.info("登录尝试: username={}", dto.getUsername());
        return userService.login(dto);
    }
}
